﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Subsonic.ViewModels;
using Subsonic.Data;
using Subsonic.Controls;
using MichMan.Utilities;

namespace Subsonic.Pages
{
    public class QueueChildItem : ChildItem
    {
        public QueueChildItem(Child child) : base(child)
        {
        }

        public override void DoAction(System.Windows.Navigation.NavigationService navService)
        {
            MusicPlayer.Instance.CurrentSong = this.Child;
        }
    }

    public class QueueViewModel : PivotItemViewModel
    {
        public override MichMan.Utilities.Requests.IRequest LoadItems()
        {
            MusicPlayer.Instance.Queue.ForEach(c => this.Items.Add(new QueueChildItem(c)));
            return null;
        }

        public int NowPlayingIndex
        {
            get
            {
                return MusicPlayer.Instance.CurrentIndex;
            }
        }

        internal void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            this.NotifyPropertyChanged("NowPlayingIndex");
        }
    }

    public class NowPlayingPivotModel : PivotViewModel
    {
        public override MichMan.Utilities.Requests.IRequest LoadPivots()
        {
            App.Current.RootFrame.LayoutUpdated += new EventHandler(RootFrame_LayoutUpdated);
            return null;
        }

        Once mediaElementAvailable = new Once(() => App.MediaElement != null);
        void RootFrame_LayoutUpdated(object sender, EventArgs e)
        {
            if (mediaElementAvailable.Value)
            {
                QueueViewModel qvm = new QueueViewModel();
                qvm.Load();
                Pivots.Add(qvm);

                App.Current.RootFrame.LayoutUpdated -= new EventHandler(RootFrame_LayoutUpdated);
                App.MediaElement.MediaOpened += new RoutedEventHandler(MediaElement_MediaOpened);
            }
        }

        void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            this.NotifyPropertyChanged("Album");
            this.NotifyPropertyChanged("Artist");
            this.NotifyPropertyChanged("Title");
            this.NotifyPropertyChanged("ItemImage");

            QueueViewModel qvm = (QueueViewModel)Pivots[0];
            qvm.MediaElement_MediaOpened(sender, e);
        }

        public string Album
        {
            get
            {
                if (MusicPlayer.Instance.CurrentSong != null)
                {
                    return MusicPlayer.Instance.CurrentSong.Album;
                }
                return null;
            }
        }

        public string Artist
        {
            get
            {
                if (MusicPlayer.Instance.CurrentSong != null)
                {
                    return MusicPlayer.Instance.CurrentSong.Artist;
                }
                return null;
            }
        }

        public string Title
        {
            get
            {
                if (MusicPlayer.Instance.CurrentSong != null)
                {
                    return MusicPlayer.Instance.CurrentSong.Title;
                }
                return null;
            }
        }

        public string ItemImage
        {
            get
            {
                if (MusicPlayer.Instance.CurrentSong != null)
                {
                    return SubsonicRequestContext.CoverArtUrl(MusicPlayer.Instance.CurrentSong.CoverArt);
                }
                return SubsonicRequestContext.CoverArtUrl(null);
            }
        }

    }

    public partial class NowPlaying : MediaApplicationPage
    {
        public NowPlaying()
        {
            InitializeComponent();
        }

        // TODO:  firstPivotId
        protected override PivotViewModel CreateViewModel(string firstPivotId)
        {
            return new NowPlayingPivotModel();
        }
    }
}